package com.example.musicplayer.fragment;

import android.os.Bundle;
import android.os.Handler;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.musicplayer.R;

public class BaseFragment extends Fragment {
    private boolean mIsStarted = false;
    private boolean mEnableTimer = false;
    private int mTimerInterval;
    private Handler mHandler;

    private final Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            if (getActivity() != null) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (onRefreshView()) {
                            mHandler.postDelayed(mRunnable, mTimerInterval);
                        }
                    }
                });
            }
        }
    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTimerInterval = getResources().getInteger(R.integer.refresh_time_middle);
    }

    @Override
    public void onStart() {
        super.onStart();
        mIsStarted = true;
    }

    @Override
    public void onStop() {
        super.onStop();
        mIsStarted = false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        setEnableTimer(false, false);
    }

    protected void setEnableTimer(boolean enable, boolean start) {
        mEnableTimer = enable;
        if (enable) {
            if (mHandler == null) {
                mHandler = new Handler();
            }
            if (start) {
                mHandler.post(mRunnable);
            }
        } else {
            if (mHandler != null) {
                mHandler.removeCallbacks(mRunnable);
                mHandler = null;
            }
        }
    }

    protected void setTimerInterval(int timerInterval) {
        if (timerInterval > 0) {
            mTimerInterval = timerInterval;
        }
    }

    protected Handler getHandler() {
        if (mHandler == null) {
            mHandler = new Handler();
        }
        return mHandler;
    }

    protected void post(Runnable action) {
        postDelayed(action, 0);
    }

    protected void postDelayed(Runnable action, long time) {
        if (action != null && time >= 0) {
            getHandler().postDelayed(action, time);
        }
    }

    protected void removeCallbacks(Runnable action) {
        if (action != null) {
            getHandler().removeCallbacks(action);
        }
    }

    protected boolean onRefreshView() {
        return true;
    }

    public boolean isEnableTimer() {
        return mEnableTimer;
    }

    public final void startTimer() {
        if (mEnableTimer) {
            if (mHandler == null) {
                setEnableTimer(true, true);
            } else {
                mHandler.removeCallbacks(mRunnable);
                mHandler.post(mRunnable);
            }
        }
    }

    public final void pauseTimer() {
        if (mEnableTimer && mHandler != null) {
            mHandler.removeCallbacks(mRunnable);
        }
    }

    public final boolean isStarted() {
        return mIsStarted;
    }
}